//
//  ServerErrorService.swift
//  blueSmart
//
//  Created by niupark on 2017/6/21.
//  Copyright © 2017年 share more. All rights reserved.
//

import UIKit
import YSKit
import TSMessages

public extension NSObject  {
    func handle(withError: Any?, targetVC:UIViewController? = nil) {
        
//        guard var vc = targetVC ?? APP.currentVC else {
//            return
//        }
        let vc = TSMessage.defaultViewController()
        guard let error = withError as? YSNetWorkError else{
            TSMessage.showNotification(in: vc, title: I18n("message.error.title"), subtitle: I18n("error.server.message.unkown"), type: .error, duration: 2.0)
            return
        }
        switch error {
            
        case .serverError(let code, let message):
            if code == -2 {
               //登录失效
                Auth.logout()
            }else {
                TSMessage.showNotification(in: vc, title: I18n("message.error.title"), subtitle: message, type: .error, duration: 2.0)

            }

        case .networkError(_, _):
            TSMessage.showNotification(in: vc, title: I18n("message.error.title"), subtitle: I18n("error.network.message"), type: .error, duration: 2.0)
            
        default:
            TSMessage.showNotification(in: vc, title: I18n("message.error.title"), subtitle: I18n("error.server.message.unkown"), type: .error, duration: 2.0)
            
            
        }
    }
}
